package com.yixin.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.entity.domain.Job;
import com.yixin.hospital.entity.domain.JobTitle;
import com.yixin.hospital.entity.dto.req.jobtitle.JobTitleAddReqDTO;
import com.yixin.hospital.entity.dto.req.jobtitle.JobTitleEditReqDTO;
import com.yixin.hospital.entity.vo.JobTitleChildrenVO;
import com.yixin.hospital.entity.vo.JobTitleListTreeVO;
import com.yixin.hospital.entity.vo.JobTitleTreeVO;
import com.yixin.hospital.entity.vo.JobTitleVO;
import com.yixin.hospital.service.JobService;
import com.yixin.hospital.service.JobTitleService;
import com.yixin.hospital.mapper.JobTitleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author wb-caifenghua
* @description 针对表【job_title(职称表)】的数据库操作Service实现
* @createDate 2024-03-22 14:34:53
*/
@Service
public class JobTitleServiceImpl extends ServiceImpl<JobTitleMapper, JobTitle>
    implements JobTitleService{

    @Resource
    JobService jobService;


    @Override
    public ApiResult allList() {
        List<JobTitle> list = this.list();
        List<JobTitleVO> jobTitleVOS = BeanUtil.copyToList(list, JobTitleVO.class);
        for (JobTitleVO jobTitleVO : jobTitleVOS) {
            Job job = jobService.getById(jobTitleVO.getJobId());
            jobTitleVO.setJobName(job.getName());
        }
        return ApiResult.success(jobTitleVOS);
    }

    @Override
    public ApiResult treeList() {
        JobTitleListTreeVO jobTitleListTreeVO = new JobTitleListTreeVO();

        List<JobTitleTreeVO> jobTitleTreeVOList = new ArrayList<>();

        List<Job> jobList = jobService.list();
        for (Job job : jobList) {
            // 职业
            JobTitleTreeVO jobTitleTreeVO = new JobTitleTreeVO();
            jobTitleTreeVO.setId(job.getId());
            jobTitleTreeVO.setLabel(job.getName());
            jobTitleTreeVO.setLevel(2);
            // 职称
            LambdaQueryWrapper<JobTitle> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(JobTitle::getJobId, job.getId());
            List<JobTitle> jobTitleList = this.list(queryWrapper);
            List<JobTitleChildrenVO> children = new ArrayList<>();
            for (JobTitle jobTitle : jobTitleList) {
                JobTitleChildrenVO childrenVO = new JobTitleChildrenVO();
                childrenVO.setId(jobTitle.getId());
                childrenVO.setLabel(jobTitle.getName());
                childrenVO.setLevel(3);
                children.add(childrenVO);
            }
            jobTitleTreeVO.setChildren(children);

            jobTitleTreeVOList.add(jobTitleTreeVO);
        }

        jobTitleListTreeVO.setChildren(jobTitleTreeVOList);

        return ApiResult.success(Arrays.asList(jobTitleListTreeVO));
    }

    @Override
    public ApiResult jobJobTitle(Long jobId) {
        LambdaQueryWrapper<JobTitle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(JobTitle::getJobId, jobId);
        List<JobTitle> list = this.list(queryWrapper);
        return ApiResult.success(list);
    }

    @Override
    public ApiResult add(JobTitleAddReqDTO jobTitleAddReqDTO, String userName) {
        JobTitle jobTitle = new JobTitle();
        jobTitle.setName(jobTitleAddReqDTO.getName());
        jobTitle.setJobId(jobTitleAddReqDTO.getJobId());
        jobTitle.setCreatedBy(userName);
        jobTitle.setUpdatedBy(userName);
        this.save(jobTitle);
        return ApiResult.success();
    }

    @Override
    public ApiResult edit(JobTitleEditReqDTO jobTitleEditReqDTO, String userName) {
        JobTitle jobTitle = new JobTitle();
        jobTitle.setId(jobTitleEditReqDTO.getId());
        jobTitle.setName(jobTitleEditReqDTO.getName());
        jobTitle.setUpdatedBy(userName);
        this.updateById(jobTitle);
        return ApiResult.success();
    }
}




